import { delArticleApi } from '@/api/cms/article/article'
import { ArticleModel, ArticleParam } from '@/api/cms/article/ArticleModel'
import useInstance from '@/hooks/useInstance'
import { StatusCode } from '@/http/request'
import { setSessionStorage } from '@/utils/sessionStore'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

export default function useArticle(getArticleTable: any) {
    const { global } = useInstance()

    const router = useRouter()

    const addEditRef = ref<{
        show: (type: string, row?: ArticleModel) => void
    }>()

    // 新增
    const addBtn = () => {
        // setSessionStorage('ArticleId', '0')
        // 跳转新增页
        router.push({ path: '/cms/article/add' })
    }

    // 编辑
    const editBtn = async (row: ArticleModel) => {
        // row.status = String(row.status)
        // addEditRef.value?.show(EditType.EDIT, row)
        setSessionStorage('ArticleId', row.id as string)
        // 跳转编辑页
        router.push({
            path: '/cms/article/edit'
        })
    }

    // 删除
    const delBtn = async (id: number) => {
        const confirm = await global.$eleConfirm('确定删除该数据吗？')
        if (confirm) {
            // 执行删除操作
            let res = await delArticleApi(
                reactive<ArticleParam>({
                    id: id
                })
            )
            if (res && res.code == StatusCode.Success) {
                // 刷新列表
                getArticleTable()
                // 信息提示
                global.$message({ message: res.msg, type: 'success' })
            }
        }
    }
    return {
        addBtn,
        editBtn,
        delBtn,
        addEditRef
    }
}
